using Gtk;

namespace Zoo
{

public class App : Window
{
    private SBoxTerminal terminal;
    private XWidget xwidget;
    private TargetSelector targetselector;

    public App()
    {
        this.title = "Zoo";
        this.position = WindowPosition.CENTER;
        this.destroy.connect(this.quit_cb);

        // Menubar
        var menubar = new MenuBar();

        var sb_menu = new MenuItem.with_mnemonic("_Scratchbox");
        menubar.append(sb_menu);

        // Toolbar
        var toolbar = new Toolbar();
        var start_btn = new ToolButton.from_stock("gtk-media-play");
        start_btn.clicked.connect(start_cb);
        toolbar.insert(start_btn, 0);
        var stop_btn = new ToolButton.from_stock("gtk-media-stop");
        stop_btn.clicked.connect(stop_cb);
        toolbar.insert(stop_btn, 1);
        var restart_btn = new ToolButton.from_stock("gtk-execute");
        restart_btn.clicked.connect(restart_cb);
        toolbar.insert(restart_btn, 2);
        var kill_btn = new ToolButton.from_stock("gtk-stop");
        kill_btn.clicked.connect(kill_cb);
        toolbar.insert(kill_btn, 3);

        // Middle panel
        var middlepanel = new HBox(false, 4);
        middlepanel.border_width = 2;

        // Scratchbox Targets
        var targets = new VBox(false, 4);

        var frame = new Frame(null);
        frame.set_shadow_type(ShadowType.IN);
        targets.pack_start(frame, true, true, 0);

        //var sbox = new Scratchbox("/home/setanta/debootstraps/maemosdk");
        var sbox = new Scratchbox("/ubuntu32");
        sbox.debootstrap_name = "ubuntu32";

        targetselector = new TargetSelector(ref sbox);
        frame.add(targetselector);

        var image = new Image.from_file("maemo.png");
        image.width_request = 300;
        image.height_request = 300;
        var iframe = new Frame(null);
        iframe.set_shadow_type(ShadowType.IN);
        iframe.add(image);
        targets.pack_start(iframe, false, false, 0);

        middlepanel.pack_start(targets, false, true, 0);

        // Create embedded X Server widget
        xwidget = new XWidget();
        GLib.Idle.add(xwidget.start_xserver);

        var xframe = new Frame(null);
        xframe.set_shadow_type(ShadowType.IN);
        xframe.add(xwidget);
        middlepanel.pack_start(xframe, true, false, 0);

        // Terminal
        terminal = new SBoxTerminal();
        terminal.child_exited.connect(on_terminal_child_exit);
        terminal.grab_focus();
        terminal.init();

        var vbox = new VBox(false, 2);
        vbox.pack_start(menubar, false, true, 0);
        vbox.pack_start(toolbar, false, true, 0);
        vbox.pack_start(middlepanel, false, true, 2);
        vbox.pack_start(terminal, true, true, 0);
        add(vbox);
    }

    ~App()
    {
        stderr.printf("destructor 1\n");
        xwidget.stop_xserver();
        stderr.printf("destructor 2\n");
    }

    public void start_cb(Widget source)
    {
        terminal.start();
    }

    public void stop_cb(Widget source)
    {
        terminal.stop();
    }

    public void restart_cb(Widget source)
    {
        terminal.restart();
    }

    public void kill_cb(Widget source)
    {
        terminal.kill_all();
    }

    /*
    public void prefs_cb(Widget source)
    {
        stderr.printf("preferences\n");
    }

    public void about_cb(Widget source)
    {
        stderr.printf("about\n");
    }

    public void on_window_delete_event(Widget widget, Gdk.EventKey e)
    {
        stderr.printf("on_window_delete_event\n");
        quit();
    }
    */

    public void on_terminal_child_exit()
    {
        stderr.printf("on_terminal_child_exit()\n");
        quit();
    }

    public void quit_cb()
    {
        stderr.printf("quit_cb()\n");
        quit();
    }

    public void quit()
    {
        stderr.printf("quit()\n");
        Gtk.main_quit();
    }

    /*
    [CCode (instance_pos = -1)]
    public static void on_target_combo_changed(ComboBox combo)
    {
        stderr.printf("on_target_combo_changed\n");
    }

    [CCode (instance_pos = -1)]
    public static void on_transp_combo_changed(ComboBox combo)
    {
        stderr.printf("on_transp_combo_changed\n");
    }

    [CCode (instance_pos = -1)]

    */
}

} // namespace Zoo

static int main(string[] args)
{
    Gtk.init(ref args);

    Zoo.App app = new Zoo.App();
    app.show_all();
    Gtk.main();

    return 0;
}

